package com.nuaa.ange.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nuaa.ange.annotation.ApiLog;
import com.nuaa.ange.entity.ArticlePic;
import com.nuaa.ange.entity.ReplyPic;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @description
 * @author 玦离
 * @date 2023/1/1 15:18
 */

@RestController
@Api(tags = "oss相关接口")
@RequestMapping("/oss")
public class OssController extends BaseController{

    @ApiOperation(value = "上传文章图片")
    @PostMapping("/sendPic")
    @ApiLog
    public void uploadOssFile (HttpServletRequest req, @RequestParam("articleId") int articleId) {

        List<ArticlePic> articlePics = articlePicService.list(new QueryWrapper<ArticlePic>()
                .eq("article_id", articleId));
        if (articlePics.size() > 0) {
            for (ArticlePic articlePic : articlePics) {
                articlePicService.removeById(articlePic.getArticlePicId());
            }
        }

        Map<String, MultipartFile> files = ((MultipartHttpServletRequest) req).getFileMap();

        for (MultipartFile file : files.values()) {
            String initialUrl = ossService.uploadFile(file);
            try {
                String path = URLEncoder.encode(initialUrl, "UTF-8");
                String url = path.replaceAll("%3A", ":").replaceAll("%2F", "/").replaceAll("\\+", "%20");
                ArticlePic articlePic = new ArticlePic();
                articlePic.setArticleId(articleId);
                articlePic.setPicUrl(url);
                articlePicService.save(articlePic);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }

    @PostMapping("/sendReplyPic")
    public void uploadOssReplyFile (HttpServletRequest req, @RequestParam("replyId") int replyId) {

        Map<String, MultipartFile> files = ((MultipartHttpServletRequest) req).getFileMap();

        for (MultipartFile file : files.values()) {
            String initialUrl = ossService.uploadFile(file);
            try {
                String path = URLEncoder.encode(initialUrl, "UTF-8");
                String url = path.replaceAll("%3A", ":").replaceAll("%2F", "/").replaceAll("\\+", "%20");
                ReplyPic replyPic = new ReplyPic();
                replyPic.setReplyId(replyId);
                replyPic.setPicUrl(url);
                replyPicService.save(replyPic);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }
}
